package com.blog.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.dto.ArticleDTO;
import com.blog.entity.Article;
import com.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping
    public ResponseEntity<Page<Article>> listArticles(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Article> articlePage = articleService.listArticles(pageNum, pageSize);
        return ResponseEntity.ok(articlePage);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Article> getArticleDetail(@PathVariable Long id) {
        Article article = articleService.getArticleDetail(id);
        if (article == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(article);
    }

    @PostMapping
    public ResponseEntity<Map<String, Object>> saveArticle(@RequestBody ArticleDTO articleDTO) {
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            return ResponseEntity.status(401).body(Map.of("message", "未登录"));
        }

        Long userId = StpUtil.getLoginIdAsLong();
        boolean success = articleService.saveOrUpdateArticle(articleDTO, userId);

        if (success) {
            return ResponseEntity.ok(Map.of("message", "保存成功"));
        } else {
            return ResponseEntity.badRequest().body(Map.of("message", "保存失败"));
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateArticle(
            @PathVariable Long id,
            @RequestBody ArticleDTO articleDTO) {
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            return ResponseEntity.status(401).body(Map.of("message", "未登录"));
        }

        articleDTO.setId(id);
        Long userId = StpUtil.getLoginIdAsLong();
        boolean success = articleService.saveOrUpdateArticle(articleDTO, userId);

        if (success) {
            return ResponseEntity.ok(Map.of("message", "更新成功"));
        } else {
            return ResponseEntity.badRequest().body(Map.of("message", "更新失败"));
        }
    }

    @GetMapping("/category/{categoryId}")
    public ResponseEntity<Page<Article>> getArticlesByCategory(
            @PathVariable Long categoryId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Article> articlePage = articleService.getArticlesByCategory(categoryId, pageNum, pageSize);
        return ResponseEntity.ok(articlePage);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteArticle(@PathVariable Long id) {
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            return ResponseEntity.status(401).body(Map.of("code",401,"message", "未登录"));
        }

        boolean success = articleService.removeById(id);

        if (success) {
            return ResponseEntity.ok(Map.of("code",200,"message", "删除成功"));
        } else {
            return ResponseEntity.badRequest().body(Map.of("code",500,"message", "删除失败"));
        }
     }
}
